/*
 * 作者: 张帆
 * 时间: 2017-04-19 
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {
	
	require("jquery");

	var c = require('app/utils/CommonUtils');
	var APIUtils = require('app/utils/APIUtils');
	var appInfo = require('app/AppInfo');
	var action_search = require('app/action/SearchIconFrmAction');
	var hc = require('app/utils/HttpClientUtils');
	var EventUtils=require('app/utils/EventUtils');
	var HashMap = require('app/utils/HashMapUtils');
	window.list=new HashMap();
	var param = api.pageParam;
	var repeatArr=[];
	var formbody={};
	formbody.rooms=[];
	formbody.fields=[];
	$("#back").click(function(){
		api.closeWin({
	    });
	});
	function changeRelation(){
		$("#detail_title").val(param.title);
		var fielddom;
		var fieldurl=appInfo.serverbase + "/luyan/form/"+param.formid+"/field_list";
		var fieldbody={};
		hc.post(fieldurl,fieldbody,function(resp){
			if(resp.status=="200"&&resp.result.next){
				resp.result.fields.forEach(function(item,key){
					fielddom=
						'<li class="aui-list-item">'+
				            '<div class="aui-list-item-inner">'+
				                '<div class="aui-list-item-title">'+item.name+
				                '</div>'+
				                '<div class="aui-btn aui-btn-danger deletename">删除</div>'+
				            '</div>'+
				        '</li>'
				 	$("#enrollname").append(fielddom);
				    $("#enrollname li").eq(key).find(".deletename").on("click",function(){
				    	api.confirm({
							title: "温馨提示",
							msg: '是否删除',
				    		buttons: ['取消', '确定']
				        },function(ret,err){
				        if(ret.buttonIndex == 2){
							$("#enrollname li").eq(key).hide();
						    formbody.fields.splice(key,1)
				        }else{
				        }})
						return;
				    })
				    formbody.fields.push({
				    	fieldname:item.name
				    })
				})
			}
		})
		
		var dom;
 		var url=appInfo.serverbase + "/luyan/form/"+param.formid+"/room_list";
 		var body={};
		hc.post(url,body,function(resp){
			if(resp.status=="200"){
				resp.result.rooms.forEach(function(item,key){
					dom='<li class="aui-list-item">'+
				            '<div class="aui-list-item-inner">'+
				                '<div class="aui-list-item-title">'+item.name+
				                '</div>'+
				                '<div class="aui-btn aui-btn-danger delete">删除</div>'+
				            '</div>'+
				        '</li>'
				    $("#enrollpro").append(dom);
				    $("#enrollpro li").eq(key).find(".delete").on("click",function(){
				    	api.confirm({
							title: "温馨提示",
							msg: '是否删除关联的直播间',
				    		buttons: ['取消', '确定']
				        },function(ret,err){
				        if(ret.buttonIndex == 2){
				        	$("#enrollpro li").eq(key).hide();
				        	formbody.rooms.splice(key,1)
				        }else{
				        }})
						return;
				    })
				    formbody.rooms.push({
				    	roomuuid:item.uuid
				    })
				    repeatArr.push(item.uuid)
				})
				
			}else{
			
			
			}
			dom='<li class="aui-list-item btnarr">'+
			         '<div class="aui-list-item-inner">'+
			        	'<div id="Preservation" style="margin-left: 120px" class="aui-btn aui-btn-primary">保存</div>'+
			            '<div id="delete" style="margin-right: 120px" class="aui-btn aui-btn-danger">删除</div>'+
			         '</div>'+
	        	'</li>'
	        $("#btn").append(dom);
	        
			$("#Preservation").on('click',function(){
				if(param.type=="change"){
					var url=appInfo.serverbase + "/luyan/form/"+param.formid+"/update";
				}else if(param.type=="add"){
					var url=appInfo.serverbase + "/luyan/form/create";
				}
				formbody.name=$("#detail_title").val()
				var body=formbody;
				hc.post(url,body,function(resp){
					if(resp.status=="200"){			
						EventUtils.send('EnrollProWin','refresh_enrollPro_list');
						api.closeWin({
						});
					}	
				})			
			})
			$("#delete").on('click',function(){
				api.confirm({
					title: "温馨提示",
					msg: '是否删除',
				    buttons: ['取消', '确定']
				},function(ret,err){
				if(ret.buttonIndex == 2){
					var url=appInfo.serverbase + "/luyan/form/"+param.formid+"/delete";
					var body={};
					hc.post(url,body,function(resp){
						if(resp.status=="200"){
							EventUtils.send('EnrollProWin','refresh_enrollPro_list');
							api.closeWin({
							});
						}
					})
				}else{
				}})


			})
		})
	}
	function addRelation(){
		formbody.rooms=[];
		var dom;
			dom='<li class="aui-list-item btnarr">'+
			         '<div class="aui-list-item-inner">'+
			        	'<div id="Preservation" style="margin-left: 120px" class="aui-btn aui-btn-primary">保存</div>'+
			            '<div id="" style="margin-right: 120px" class="aui-btn aui-btn-danger">删除</div>'+
			         '</div>'+
	        	'</li>'
	        $("#btn").append(dom);
	        $("#Preservation").on('click',function(){
				if(param.type=="change"){
					var url=appInfo.serverbase + "/luyan/form/"+param.formid+"/update";
				}else if(param.type=="add"){
					var url=appInfo.serverbase + "/luyan/form/create";
				}
				formbody.name=$("#detail_title").val()
				var body=formbody;
				hc.post(url,body,function(resp){
					if(resp.status=="200"){			
						EventUtils.send('EnrollProWin','refresh_enrollPro_list');
						api.closeWin({
						});

					}	
				})			
			})
	}
	if(param.type=="change"){
		changeRelation()
	}
	if(param.type=="add"){
		addRelation()
	}

	$("#add_button").click(function(){
		action_search.openEnrollRelationSearch();
	});

	$("#add_name").click(function(){
		var dialog = new auiDialog({})
		dialog.prompt({
                    title:"弹出提示",
                    text:'默认内容',
                    buttons:['取消','确定']
                },function(ret){
                    if(ret.buttonIndex == 2){
                    	if($(".aui-dialog-body input").val()==""){
                    		alert("姓名不能为空");
                    		return
                    	}
                    	var dom='<li class="aui-list-item name">'+
									 '<div class="aui-list-item-inner">'+
									       '<div class="aui-list-item-title">'+$(".aui-dialog-body input").val()+
									       '</div>'+
									       '<div class="aui-btn aui-btn-danger deletename">删除</div>'+
									 '</div>'+
								'</li>'
                    	$("#enrollname").append(dom);
                    	var key=$("#enrollname li").length;
                    	formbody.fields.push({fieldname:$(".aui-dialog-body input").val()})
                    	$("#enrollname li:last").find(".deletename").on("click",function(){
                    	    
                    		api.confirm({
								title: "温馨提示",
								msg: '是否删除',
							    buttons: ['取消', '确定']
							},function(ret,err){
							if(ret.buttonIndex == 2){
								$("#enrollname li").eq(key-1).hide();
						        formbody.fields.splice(key-1,1)
							}else{
							}})
                    	})

                }
        })
	})
	EventUtils.listen(api.winName, 'refresh_enrollRelation_list', false, function(data){
		if(JSON.stringify(formbody.rooms).indexOf(JSON.stringify({roomuuid:data.roomuuid}))!=-1){
			alert("不能重复添加")
			return
		}
		dom='<li class="aui-list-item">'+
				 '<div class="aui-list-item-inner">'+
				       '<div class="aui-list-item-title">'+data.roomname+
				       '</div>'+
				       '<div class="aui-btn aui-btn-danger delete">删除</div>'+
				 '</div>'+
			'</li>'
	    $("#enrollpro").append(dom);
	    formbody.rooms.push({
		    roomuuid:data.roomuuid
		})
		repeatArr.push(data.roomuuid);
		var key=$("#enrollpro li").length;
	    $("#enrollpro li:last").find(".delete").on("click",function(){
			api.confirm({
				title: "温馨提示",
				msg: '是否删除关联的直播间',
				buttons: ['取消', '确定']
				},function(ret,err){
				    if(ret.buttonIndex == 2){
				        $("#enrollpro li").eq(key-1).hide();
				        formbody.rooms.splice(key-1,1)
				    }else{
				    }})
					return;
	    })
	});
	
	return;
});